<?php

namespace App\Controller\Admin;

use App\Controller\Example\UserExample;
use App\Models\Conn;
use Core\Common\Common;
use Core\Result\Result;

class AppUpdate
{
    /**
     * app更新
     * entry token,version,startTime,endTime,page,limit
     * return json
     * */
    public function appUpdateList($data){
        $assemble = Common::assemble(new UserExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE 1";
        if($assemble->version !=null){ $joinSql.=" and `version` ='$assemble->version' ";}
        if($assemble->startTime !=null){ $assemble->startTime = strtotime($assemble->startTime); $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $assemble->endTime = strtotime($assemble->endTime); $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $sql = "SELECT `id`, `version`, `content`, `addr`, `time` FROM `appupdate` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($sql,true);
        $returnData["list"]=$readConn;
        $countSql="SELECT count(id) as count FROM `appupdate` $joinSql";
        $returnData["count"] = $conn->readConn($countSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
    /**
     * 添加app更新
     * entry token,version,content,addr
     * return json
     * */
    public function addUpdate($data){
        $version = $data["version"];
        $content = $data["content"];
        $addr = $data["addr"];
        if(empty($version) || empty($content) || empty($addr)){
            Result::error("参数不完整");
        }
        $conn = new Conn();
        $insertSql = "insert into `appupdate`(`version`,`content`,`addr`,`time`) values ('$version','$content','$addr',now())";
        $conn->shiwuStart();
        if($conn->executeConn($insertSql) !=1){
            $conn->shiwuRollback();
            Result::error("失败");
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
}